import Vue from 'vue'
import App from './App.vue'
import '@/assets/css/bootstrap.css'

Vue.config.productionTip = false

// 导入vuex
import Vuex from "vuex"

// 装插件
Vue.use(Vuex)

// 创建store的实例对象
let store = new Vuex.Store({
  // Vuex的配置选项
  state: {
    count: 0,
    list: [
      {id:1,task:"吃饭",done:false},
      {id:2,task:"睡觉",done:true},
      {id:3,task:"吃饭",done:false}
    ]
  },
  mutations: {
    add(state, payload) {
      state.count += payload.num
    },
    sub(state, payload) {
      state.count += payload.num
    }
  },
  actions: {
    addAsync(context) {
      // 执行异步操作
      setTimeout(() => {
        context.commit("add", {
          num: 1
        })
      }, 1000)
    },
    subAsync(context) {
      // 执行异步操作
      setTimeout(() => {
        context.commit("sub", {
          num: -1
        })
      }, 1000)
    }
  },
  getters: {
    isAll(state) { 
      return state.list.every(item=>item.done===true)
    }
  }
})

new Vue({
  render: h => h(App),
  store
}).$mount('#app')